package it.com.chen.movieReleasesDB;

import java.util.Calendar;
import java.util.HashMap;

public abstract class UpdateManager {

	public static ProgramData updateDB(ProgramData programData){
		
		HashMap<Integer, DBEntry> db = programData.getDb();
		int updateDate = programData.getUpdateDate();
		int updateMonth = programData.getUpdateMonth();
		int updateYear = programData.getUpdateYear();
		
		Calendar today = Calendar.getInstance();
		Calendar lastUpdate = Calendar.getInstance();
		lastUpdate.set(updateYear, updateMonth, updateDate);
		Calendar consideredDate = Calendar.getInstance();
		consideredDate.set(updateYear, updateMonth, updateDate);
		consideredDate.add(Calendar.DATE, 1);
		while(consideredDate.after(lastUpdate) && consideredDate.before(today)){
			String consideredMonth = CalendarMonthManager.convertMonth(consideredDate.get(Calendar.MONTH));
			int consideredYear = consideredDate.get(Calendar.YEAR);
			
			db = OnlineDBReader.populateDB(consideredMonth, consideredYear, db, lastUpdate);
			consideredDate.add(Calendar.MONTH, 1);
		}
		ProgramData newData = new ProgramData(db, today.get(Calendar.DAY_OF_MONTH), today.get(Calendar.MONTH), today.get(Calendar.YEAR));
		return newData;
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		HashMap<Integer, DBEntry> db = new HashMap<>();
		ProgramData data = new ProgramData(db, 1, 8, 2012);
		data = updateDB(data);
		int updateDate = data.getUpdateDate();
		int updateMonth = data.getUpdateMonth();
		int updateYear = data.getUpdateYear();
		System.out.println("# New update date \""+updateDate+"/"+CalendarMonthManager.convertMonth(updateMonth)+"/"+updateYear);
		db = data.getDb();
		MovieListPrinter.print(db);
	}

}
